<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        #son {
            position: absolute;
            left: 0;
            background-color: red;
            width: 100px;
            height: 100px;
            top: 100px;
        }
    </style>

</head>

<body>

    <div id="son"></div>
    <button>到500</button>
    <button>到800</button>

</body>

<script>
    var son = document.getElementById('son');
    var button = document.getElementsByTagName('button');

    function dhhs(obj, targer) {
        clearInterval(obj.s);
        obj.s = setInterval(function() {
            var speed = (targer - obj.offsetLeft) / 8;
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
            if (obj.offsetLeft == targer) {
                clearInterval(obj.s);
            } else {
                console.log(speed);
                obj.style.left = obj.offsetLeft + speed + 'px';
            }
        }, 18);
    }

    button[0].addEventListener('click', function() {
        dhhs(son, 500);
    })


    button[1].addEventListener('click', function() {
        dhhs(son, 800);
    })
</script>

</html>